package com.example.mobile1931605066wzq0529;

import android.os.Bundle;
import android.webkit.JavascriptInterface;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.google.gson.JsonArray;

public class NewsActivity extends AppCompatActivity {


    private WebView wvNewsList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);
        initView();
        wvNewsList.loadUrl("file:////android_asset/newslisr.html");
        wvNewsList.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(request.getUrl().toString());
                return true;
            }
        });
        wvNewsList.getSettings().setJavaScriptEnabled(true);
        wvNewsList.addJavascriptInterface(new JsNewInterface(), "obj");
    }

    class JsNewInterface {
        @JavascriptInterface
        public String getTitles() {
            JsonArray jsonArray = new JsonArray();
            jsonArray.add("标题1");
            jsonArray.add("标题1");
            jsonArray.add("标题1");
            Gson gson = new Gson();
            String str = gson.toJson(jsonArray);
            return str;
        }
    }

    private void initView() {

        wvNewsList = (WebView) findViewById(R.id.wvNewsList);
    }
}
